package com.jason.sms.agent.provider;


import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.util.Properties;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.codec.digest.DigestUtils;

import com.jason.sms.agent.model.SmsRequest;
import com.jason.sms.agent.util.PhoneNumberUtil;

public class KuanleProvider implements SmsProvider{
	
	private static final String configPath = "/kuanle.properties";
	
	private static final String RemoteUrl = "kuanle.remoteUrl";
	
	private static final String LocalUrl = "kuanle.localUrl";
	
	private static final String User = "kuanle.user";
	
	private static final String Pwd = "kuanle.pwd";
	
	private static final String Uc = "kuanle.uc";
	
	private static final String UcPwd = "kuanle.ucpwd";
	
	
	private static String remoteUrl;
	private static String localUrl;
	private static String username;
	private static String pwd;
	public static String uc;
	public static String ucPwd;
	
	static{
		Properties property = new Properties();
		try{
			property.load(KuanleProvider.class.getResourceAsStream(configPath));
		}catch(Exception e){
			e.printStackTrace();
		}
		remoteUrl = property.getProperty(RemoteUrl);
		localUrl = property.getProperty(LocalUrl);
		username = property.getProperty(User);
		pwd = property.getProperty(Pwd);
		uc = property.getProperty(Uc);
		ucPwd = property.getProperty(UcPwd);
	}

	public long sendSms(SmsRequest req) {
		KuanleHelper helper = new KuanleHelper();
	
		String connId=helper.setCallBackAddr(helper.getRandom());
		System.out.println("MessageAction connId: " + connId);		
		int msgid = 1;
		String callee[]=new String[req.getDesAddress().size()];
		req.getDesAddress().toArray(callee);
		String res = helper.sendSMS(helper.getRandom(), connId, callee, helper.getEncoderStr(req.getContent()), msgid);
		System.out.println(res + " 短信息加执结果 ");
		
		
		return 0;
	}

	class KuanleHelper{
		
		
		private String REGISTER = "Register";
		private String CALLBACK = "CallBack";
		private String SENDSMS="SendSMS";
		private String SENDSMSMETHOD="sendSMS";
		private String CALLBACKMETHOD="EchoOfSendSMS";
		private String GETRANDOM = "GetRandom";
		private String SETCALLBACKADDR = "setCallBackAddr";
		
		public String invokeValue(String username,String password,String url,String qName,String qValue,Object[] object){
			String str=null;
			Service service = new Service();		
			Call call;
			try {
				call = (Call)service.createCall();
				call.setUseSOAPAction(true);		
				call.setUsername(username);
				call.setPassword(password);		
				call.setTargetEndpointAddress(url);
				call.setOperationName(new QName(qName,qValue));//接口级认证
				str =(String)call.invoke( object);
			} catch (Exception e) {
				str="Exception";
			} 	
			return str;
		}
		
		/**
		 * 获取随机数
		 * @throws MalformedURLException 
		 */
		public String getRandom(){
			Object []object=null;
			String random = invokeValue(username, pwd, (remoteUrl + REGISTER), REGISTER, GETRANDOM,object);		
			return random;
		}
		
		/**
		 * 该接口用于第三方服务器注册它的回调WebService接口的地址，获取连接id
		 * */	
		public String setCallBackAddr(String random){
			Object []object = {uc,DigestUtils.md5Hex(random + ucPwd+ucPwd),random,localUrl};		
			String connId = invokeValue(username, pwd,(remoteUrl + REGISTER), REGISTER, SETCALLBACKADDR, object);
			return connId;
		}
		
		
		
		public String sendSMS(String random,String connId,String callee[],String content,int msgid){		
			Object []object = {uc,DigestUtils.md5Hex(random + ucPwd+ucPwd),random,callee,"1",content,msgid,connId};
			String res = invokeValue(username, pwd,(remoteUrl + SENDSMS), SENDSMS, SENDSMSMETHOD, object);
			return res;
		}
		
		/**
	     * 提供正常字符串的BASE64加密
	     */
	    public String getEncoderStr(String str_to_base64) {
	        String str = (new sun.misc.BASE64Encoder()).encode(str_to_base64.getBytes());
	        return str;
	    }
		
	}
	
}
